package com.lk.springboot.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lk.springboot.dao.SitesMapper;
import com.lk.springboot.pojo.SitesRu;
import com.lk.springboot.pojo.UmDonation;
import com.lk.springboot.service.SitesService;
import com.lk.springboot.utils.UniversalUtil;
import com.lk.springboot.vo.Result;
import com.lk.springboot.vo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author lk
 * @date 2020/1/12 0012 15:09
 */
@Service
public class SitesServiceImpl implements SitesService {

    @Autowired
    SitesMapper sitesMapper;

    @Override
    public String getSummary() {
        List<SitesRu> list = sitesMapper.getSitesSummary();
        Result result = new Result(ResultCode.SUCCESS);
        ArrayList<JSONObject> siruList = new ArrayList<JSONObject>();
        for (SitesRu sitesRu : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("siru_id", sitesRu.getSiru_id());
            jsonObject.put("sites_num", sitesRu.getSites_num());
            jsonObject.put("detail", sitesRu.getDetail());
            jsonObject.put("donation_point", sitesRu.getPoint());// 火爆指数：1-6
            siruList.add(jsonObject);
        }
        result.setData(siruList);
        return JSON.toJSONString(result);
    }

    @Override
    public String getDetail(String siru_id) {
        JSONObject returnJson = new JSONObject();
        SitesRu sitesRu = sitesMapper.getSitesDetail(siru_id);
        int sitesNum = sitesRu.getSites_num();
        returnJson.put("sitesNum", sitesNum);
        returnJson.put("photoNum", sitesNum * 4);//站点相当于的拼图数
        returnJson.put("scoreNum", sitesNum * 200);//站点相当于的积分数
        returnJson.put("detail", sitesRu.getDetail());
        returnJson.put("umbrellaNum", sitesMapper.getUMCount());//所有人已捐赠伞总数
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(returnJson);
        return JSON.toJSONString(result);
    }

    @Override
    public String sitesExchange(String u_id, String siru_id) {
        Result result = null;
        SitesRu sitesRu = sitesMapper.getSitesDetail(siru_id);
        int umbres = sitesRu.getSites_num();//兑换所需要的站点的数量
        List<String> list = sitesMapper.getAllokSP(u_id); //获取用户已有的能用于兑换的站点列表
        if (umbres > list.size()) {//判断站点数量，是否足够兑换
            result = new Result(ResultCode.DONNOT_ENOUGH);
            result.setData("站点数量不够");
            return JSON.toJSONString(result);//不足兑换
        } else {//足够兑换
            //根据规则，循环将指定用户，站点，的一套拼图减去
            for (int i = 0; i < umbres; i++) {
                sitesMapper.reduceSites(u_id, list.get(i));
            }
            //增加捐赠伞记录
            //------------------取当地时间，插入捐伞记录----------
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(calendar.getTime());
            //---------------------------------------------------
            UmDonation umDonation = new UmDonation(u_id, sitesRu.getUm_id(), date);
            sitesMapper.addDonateUM(umDonation);
            sitesMapper.signUmbrella(u_id);  //标记捐伞任务完成，任务进度标记为1，完成
            sitesMapper.updateNewsYes(u_id);  //更新通知列表状态，更新为有新消息未读
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("user_name", sitesMapper.getUName(u_id));
            jsonObject.put("date", date);
            jsonObject.put("sites_name", sitesMapper.getPlace(sitesRu.getUm_id()));
            jsonObject.put("um_id", sitesRu.getUm_id());
            result = new Result(ResultCode.SUCCESS);
            result.setData(jsonObject);
            return JSON.toJSONString(result);
        }

    }

    @Override
    public int getBestSitesNum(String u_id) {
        List<String> list = sitesMapper.getAllokSP(u_id);
        int num = 0;
        for (String s_id : list) {
            if (sitesMapper.getAllokSPNum(u_id, s_id) != null)
                num += sitesMapper.getAllokSPNum(u_id, s_id).intValue();
        }
        return num;
    }

}